<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css" />
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">

                <div class="layui-form-item">
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-inline">
                        <input type="text" id="username" name="username" lay-verify="required" autocomplete="off" placeholder="请输入用户名" required="required" class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        <span id="msg"></span>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-inline">
                        <input type="password"  name="password" lay-verify="required|password" autocomplete="off" placeholder="请输入密码"  required="required"  class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        6到12个字符
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">确认密码</label>
                    <div class="layui-input-inline">
                        <input type="password"  name="checkPassword" lay-verify="required|checkPassword" autocomplete="off" placeholder="请再次输入密码确认"  required="required"  class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        确认密码
                    </div>
                </div>

                
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
layui.use(['form','jquery'],function(){
    let form = layui.form;
    let $ = layui.jquery;
    
    let flag = false;

    // 检查用户名
    $("#username").on("change",function () {
        $.ajax({
            type: 'post', // 提交方式 get/post
            url: '/user/checkUserName', // 需要提交的 url  检查用户名是否可用
            data: {
                username: $('input[name=username]').val()
            },
            success:function(result){
                if (result){
                    $("#msg").text("用户名可用");
                    flag = true;
                }else {
                    $("#msg").text("用户名不可用");
                    flag = false;
                }
            }
    })
    });

    // 自定义验证
    form.verify({
        password: [/(.+){6,12}$/, '密码必须6到12位']
        ,checkPassword: function (value) {
            if ($('input[name=password]').val() !== value){
                return '两次密码不一致';
            }
        }
    });

    form.on('submit(user-save)', function(data){
        if (!flag){
            return; // 用户名不可用
        }
        $.ajax({
            type: 'post', // 提交方式 get/post
            url: '/user/register', // 需要提交的 url
            data: data.field,
            success:function(result){
                if (result.code === 200){
                    layer.msg(result.message, {icon: 1, time: 500}, function () {
                        parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                        parent.layui.table.reload("user-table");
                    });
                }else {
                    layer.msg(result.message, {icon: 2, time: 500}, function () {
                        parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                        parent.layui.table.reload("user-table");
                    });
                }
            },
            error:function (result) {
                layer.msg(result.message,{icon:2,time:500},function(){
                    parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                });
                console.log("error"+result)
            }
        });
        return false;
    });
})
</script>
<script>
</script>
</body>
</html>